package com.xm.util;

import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.AlipayConfig;
import com.alipay.api.domain.AlipayTradePagePayModel;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.alipay.api.response.AlipayTradePagePayResponse;
import com.alipay.api.response.AlipayTradeWapPayResponse;
import com.alipay.api.domain.AlipayTradeWapPayModel;
import com.alipay.api.request.AlipayTradeWapPayRequest;
import lombok.SneakyThrows;

/**
 * 支付宝支付功能
 * @author 康文程
 * @date 2023-12-29 上午 10:41
 */
public class PayUtil {
    /**
     * 二维码支付
     * @param price 价格
     * @return 二维码HTML
     */
    @SneakyThrows
    public static String payPage(String price,String orderId,String accountId,String informationId){
        String privateKey = "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCC9qVv05hqm8Iy1CigUCv7XpXCK/u5fo8McQ+vDdRxrIvOwNKKe6mwCyDol9Me39yQxDp1gufbfsEjQHV1y0GNmsmB4Cl7dHAPLUddMy1YgUzxyUnWf6MJZA3KYjHSIZjBDKSn29VxBXXgLug8fNllxJ3L1wagCcQENlYMICJDkM/lqTB8KUS82lWmSQK/LASvDCLA54NGio8EFO+Z1/g3dPVpyQCfoZZw/FkUpXCv6vMN56DSTxdNL5dT1R2CendpSKJSDb6KSEc7LJowuQDKghyU6Ypu43r20A0hSrUqzAk0vVBt9gY6A+uhuJCHI6StAuleFEe8D7BB9Sb/Ai4LAgMBAAECggEAZIDP2Q+zKi06TTOPseeUuSWPHCIQ3fuH3+5sMNUWMd5OtG45pNjeEiGYMadB97GoXNiaXFAHiuStZ1YyTiGbvwYImkjizMFAtgTzfUynAgLIf80PgzMUdUJ5atUMkswBpDUFr2P/Wurr2Q1mX/UYCQdoaWQsy2hzKyBfzJJkeSBuIi8Ch0/8pi4fWFGFMa/qNDFyodSr54+mI6gd/0iELOKlMShLIliXJYgtNHury5X4Og62TCSKKj1syH+6X2JBxY1lQYD7TB2zc2qnfdRZ6O4l2+A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Nh4c8n2ud99q3qzc8Oxmf8a6s+JFecjYc5gVa9qYXvkO+UTkTxgPgEuVExA75hgM7Eu54JeLrAEd2IqIs85RKeA6wz22vsIx32wyuLxAV1SY8aspLvLrrg7livhBTl5uDuNzxbjHiqIc3klvsIcB09G55RaM7pqIevpqSIrhGND7UIsjR4ibYFybDaCwC34cu3M1GFGwRUm9EuxCnHHxCf5tL/65ibMU4vRElUtfx3sM6CETmE357sQJwLnGjsUj5ClcRo6e+P6AowIDAQAB";
        AlipayConfig alipayConfig = new AlipayConfig();
        alipayConfig.setServerUrl("https://openapi-sandbox.dl.alipaydev.com/gateway.do");
        alipayConfig.setAppId("9021000133628542");
        alipayConfig.setPrivateKey(privateKey);
        alipayConfig.setFormat("json");
        alipayConfig.setAlipayPublicKey(alipayPublicKey);
        alipayConfig.setCharset("UTF-8");
        alipayConfig.setSignType("RSA2");
        AlipayClient alipayClient = new DefaultAlipayClient(alipayConfig);
        AlipayTradePagePayRequest request = new AlipayTradePagePayRequest();
        AlipayTradePagePayModel model = new AlipayTradePagePayModel();
        model.setOutTradeNo(orderId);
        model.setTotalAmount(price);
        model.setSubject("商品购买");
        model.setProductCode("FAST_INSTANT_TRADE_PAY");
        request.setReturnUrl("http://localhost:8083/www.xiaomi.com/payment/was/successful?accountId="+
                accountId+"&orderId="+orderId+"&informationId="+informationId);
        request.setBizModel(model);
        AlipayTradePagePayResponse response = alipayClient.pageExecute(request, "POST");
        String pageRedirectionData = response.getBody();
        System.out.println(pageRedirectionData);
        if (response.isSuccess()) {
            System.out.println("调用成功");
        } else {
            System.out.println("调用失败");
        }
        return pageRedirectionData;
    }
}
